<?php
/** @var array|null $user */
/** @var array $config */

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '/';


$userGroups = [
    [
        'group' => '产品导航',
        'links' => [
            ['label' => '首页', 'href' => '/'],
            ['label' => '策略市场', 'href' => '/strategies'],
            ['label' => '订单与资产', 'href' => '/orders'],
        ],
    ],
    [
        'group' => '个人中心',
        'links' => [
            ['label' => '资产总览', 'href' => '/portfolio'],
            ['label' => '安全设置', 'href' => '/settings'],
        ],
    ],
];

$adminGroups = [
    [
        'group' => '管理驾驶舱',
        'links' => [
            ['label' => '全局总览', 'href' => '/admin/overview'],
            ['label' => '策略上架', 'href' => '/admin/strategies'],
            ['label' => '用户订阅', 'href' => '/admin/subscriptions'],
        ],
    ],
    [
        'group' => '运营工具',
        'links' => [
            ['label' => '一键广播', 'href' => '/admin/broadcast'],
            ['label' => '风控与审计', 'href' => '/admin/risk'],
        ],
    ],
];

$links = $user ? $userLinks : [];
if ($user && ($user['role'] ?? '') === 'admin') {
    $links = array_merge($userLinks, [['label' => '—', 'href' => '#']], $adminLinks);
}
?>
<header class="app-header">
    <div class="app-container nav-bar">
        <a class="brand" href="/">
            <img src="/assets/img/logo-bskpro.svg" alt="币胜客Pro 标志">
            <span><?= htmlspecialchars($config['app_name']) ?></span>
        </a>
        <nav class="nav-links">
            <?php if ($user): ?>
                <?php $groups = $userGroups; ?>
                <?php if (($user['role'] ?? '') === 'admin') {
                    $groups = array_merge($groups, $adminGroups);
                } ?>
                <?php foreach ($groups as $group): ?>
                    <div class="nav-group">
                        <span class="nav-group__title"><?= htmlspecialchars($group['group']) ?></span>
                        <div class="nav-group__links">
                            <?php foreach ($group['links'] as $link): ?>
                                <?php $active = $path === $link['href'] || str_starts_with($path, $link['href'] . '/'); ?>
                                <a href="<?= $link['href'] ?>" class="<?= $active ? 'active' : '' ?>"><?= htmlspecialchars($link['label']) ?></a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="nav-group">
                    <span class="nav-group__title">产品</span>
                    <div class="nav-group__links">
                        <a href="/" class="<?= $path === '/' ? 'active' : '' ?>">首页</a>
                        <a href="/strategies" class="<?= str_starts_with($path, '/strategies') ? 'active' : '' ?>">策略市场</a>
                        <a href="/login" class="<?= $path === '/login' ? 'active' : '' ?>">登录</a>
                    </div>
                </div>
            <?php endif; ?>
        </nav>
        <div>
            <?php if ($user): ?>
                <span style="margin-right:16px;color:var(--text-secondary);"><?= htmlspecialchars($user['email']) ?></span>
                <a class="btn-ghost btn" href="/logout">退出</a>
            <?php else: ?>
                <a class="btn-ghost btn" href="/register">注册体验</a>
                <a class="btn" style="margin-left:12px;" href="/login">立即登录</a>
            <?php endif; ?>
        </div>
    </div>
</header>
